﻿using Decoration.Models;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Decoration.Api.Comm
{
    public class MyAuthorize : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            var authHeader = from t in context.HttpContext.Request.Headers
                             where t.Key == "Authorization"
                             select t.Value.FirstOrDefault();

            if (authHeader != null)
            {
                string token = authHeader.FirstOrDefault();

                if (!string.IsNullOrEmpty(token))
                {
                    try
                    {
                        token = token.Replace("Bearer ", "");
                        ///验证签名
                        var Claims = JwtHelper.ValidateToken(token);
                        if (Claims != null)
                        {
                            var obj = JwtHelper.GetToken<AuthInfo>(token);
                            if (obj != null)
                            {
                                if (!context.RouteData.Values.ContainsKey("auth"))
                                {
                                    //把数据存在RouteData里进行传递。
                                    context.RouteData.Values.Add("auth", obj);
                                }
                            }
                            else
                            {
                                var content = new Result
                                {
                                    code = -1,
                                    msg = "服务端拒绝访问：令牌错误"
                                };
                                // 
                                //返回鉴权失败
                                context.Result = new StatusCodeResult(401);
                                context.HttpContext.Response.StatusCode = 401;
                                context.HttpContext.Response.WriteAsJsonAsync(content);
                            }
                        }
                    }
                    catch (Exception ex)
                    {

                        var content = new Result
                        {
                            code = -1,
                            msg = "服务端拒绝访问：令牌异常"
                        };

                        //返回鉴权失败
                        context.Result = new StatusCodeResult(401);
                        context.HttpContext.Response.StatusCode = 401;
                        context.HttpContext.Response.WriteAsJsonAsync(content);

                    }
                }
                else
                {
                    var content = new Result
                    {
                        code = -1,
                        msg = "服务端拒绝访问：你没有权限，或者掉线了"
                    };
                    //返回鉴权失败
                    context.Result = new StatusCodeResult(401);
                    context.HttpContext.Response.StatusCode = 401;
                    context.HttpContext.Response.WriteAsJsonAsync(content);
                }
            }
            else
            {
                var content = new Result
                {
                    code = -1,
                    msg = "服务端拒绝访问：你没有权限，或者掉线了"
                };
                //返回鉴权失败
                context.Result = new StatusCodeResult(401);
                context.HttpContext.Response.StatusCode = 401;
                context.HttpContext.Response.WriteAsJsonAsync(content);
            }
        }
    }
}
